package com.bw.ods;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import com.bw.fun.DimSinkFunc;
import com.bw.fun.MyJsonSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class XT1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        DebeziumSourceFunction<String> source = MySQLSource.<String>builder()
                .hostname("hadoop120")
                .port(3306)
                .databaseList("gmall") // set captured database
                //.tableList("yourDatabaseName.yourTableName") // set captured table
                .username("root")
                .password("123456")
                .deserializer(new MyJsonSchema()) // converts SourceRecord to JSON String
                .build();

        DataStreamSource<String> stream = env.addSource(source);
        //分流
        //维度数据
        OutputTag<String> tag = new OutputTag<String>("hbase"){};
        SingleOutputStreamOperator<String> process = stream.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String s, ProcessFunction<String, String>.Context context, Collector<String> collector) throws Exception {
                JSONObject json = JSON.parseObject(s);
                if (("base_dic,sku_info,spu_info,user_info,base_region,coupon_info,activity_sku," +
                        "coupon_range,activity_info,activity_rule,base_province," +
                        "base_category1,base_category2,base_category3,base_trademark,financial_sku_cost").contains(json.getString("tb"))) {//
                    context.output(tag,s);
                }else {
                    collector.collect(s);
                }
            }
        });

        //提取侧流保存到hbase
        DataStream<String> sideOutput = process.getSideOutput(tag);
        sideOutput.addSink(new DimSinkFunc());


        env.execute();
    }
}
